/**
 * 上海中赢金融信息服务有限公司
 * Copyright (c) 2016-2026 hmc,Inc.All Rights Reserved.
 */
package com.sys.midware.hive.config;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import com.sys.midware.config.core.ConfigContainer;
import com.sys.midware.config.core.ConfigKey;
import com.sys.midware.config.model.BNode;
import com.sys.midware.logger.Logger;
import com.sys.midware.logger.LoggerFactory;

/**
 * 
 * <b>ClassName：</b> ConfigMongoUtil <br/>
 * <b>Description：</b> 获取配置信息工具类 <br/>
 * <b>@author：</b> mobing <br/>
 * <b>@date：</b> 2017年1月24日 下午2:55:02 <br/>
 * <b>@version: </b> <br/>
 */
public class BHiveConfigUtil {
    private static final Logger logger = LoggerFactory.getLogger(BHiveConfigUtil.class);

    /**
     * ConfigContainer instance
     */
    private static ConfigContainer cfg = ConfigContainer.getInstance(ConfigKey.ConfigFile.CONFIG_FILE_ROOT);

    private static boolean inited;

    public static void initConfig() {
        if (inited) {
            return;
        }
        logger.info("开始初始化hive配置...");
        
        Map<String, BHiveConfig> configCached = new HashMap<String, BHiveConfig>();
        Map<String, DataSource> dataSourceCached = new HashMap<String, DataSource>();
        List<BNode> bnodeList = cfg.getBNodeList();
        for (BNode b : bnodeList) {
            String nodeFor = b.getNodeFor();
            if("hive".equals(nodeFor)){
                BHiveConfig config = new BHiveConfig();
                config.setNodeName(b.getNodeName());
                config.setNodeType(b.getNodeType());
                
                //设置链接池属性
                 PoolConfig poolConfig =new PoolConfig();
                fillPoolProperties(poolConfig,b);
                config.setPoolConfig(poolConfig);
                config.setDriverClassName(b.getNodeParamStr(BHiveConfigKey.KEY_DRIVER_CLASS_NAME));
                config.setUrl(b.getNodeParamStr(BHiveConfigKey.KEY_URL));
                config.setUsername(b.getNodeParamStr(BHiveConfigKey.KEY_USERNAME));
                config.setPassword(b.getNodeParamStr(BHiveConfigKey.KEY_PASSWORD));
                configCached.put(b.getNodeName(), config);
//                dataSourceCached.put(b.getNodeName(), dataSource);
            }
        }
        BHiveConfigMapHolder.put(configCached);
//        BHiveDatasourceHolder.put(dataSourceCached);
        inited = true;
        logger.info("hive config has bean inited!");
    }

    
    /**
     * 填充连接池属性配置
     * @param element
     * @param node
     * @return
     */
    private static PoolConfig fillPoolProperties(PoolConfig poolConfig,BNode node) {
        /*
         * 连接池管理
         */
        poolConfig.setInitialSize(cfg.getParamInteger(BHiveConfigKey.KEY_POOL_initialSize));
        poolConfig.setMaxActive(cfg.getParamInteger(BHiveConfigKey.KEY_POOL_maxActive));
        poolConfig.setCheckPool(cfg.getParamBoolean(BHiveConfigKey.KEY_POOL_isCheckPool));
        poolConfig.setCheckDelayMilSecond(cfg.getParamLong(BHiveConfigKey.KEY_POOL_checkDelayMilSecond));
        poolConfig.setCheckPeriodMilSecond(cfg.getParamLong(BHiveConfigKey.KEY_POOL_checkPeriodMilSecond));
        return poolConfig;
    }
    
    
   
    
}
